/***************************************************************************************
* Copyright (c) 2014-2022 Zihao Yu, Nanjing University
*
* NEMU is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*          http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
*
* See the Mulan PSL v2 for more details.
***************************************************************************************/

#include <common.h>
#include "../monitor/sdb/sdb.h"
void init_monitor(int, char *[]);
void am_init_monitor();
void engine_start();
int is_exit_status_bad();

int main(int argc, char *argv[]) {

  /* Initialize the monitor. */
#ifdef CONFIG_TARGET_AM
  am_init_monitor();
#else
  init_monitor(argc, argv);
#endif
//#define TEST_EXPRESSION
#ifdef TEST_EXPRESSION
	char buf1[1024] = {};
	char *fgets_p;
	FILE *f = fopen("/home/zc/ysyx/nemu/tools/gen-expr/input","r");	
	int test_num = 0;
	int too_long_num = 0;
	int success_num = 0;
	if(f == NULL){printf("can't open file\n");return 0;}
	while(!feof(f)){
		memset(buf1,'\0',sizeof(buf1));
		fgets_p = fgets(buf1,sizeof(buf1),f);
		buf1[0+strlen(buf1)-1] = '\0';//change the last \n to \0;
		if(fgets_p != NULL)
		{
			char *result_s = strtok(buf1," ");	
			char *pt;
			word_t  result = strtol(result_s,&pt,10); 
			//int result  = atoi(result_s); 
			char *expression = buf1 + strlen(result_s) + 1;
			bool success = true;
			printf("%s\n",expression);
			word_t answer;
			answer = expr(expression,&success);
			printf("answer = %ld\n",result);
			printf("expression = %s\n\n",expression);
			test_num ++;
			if(success == false) {printf("expression is too long\n");too_long_num++;}
			else if (answer == result)success_num ++;
			else {return 0;}
		}
}
	fclose(f);
	printf("test %d times\n",test_num);
	printf("%d expressions are too long\n",too_long_num);
	printf("%d tests successed\n",success_num);
	printf("%d tests failed\n",test_num -too_long_num -success_num );
#endif
  /* Start engine. */
  engine_start();

  return is_exit_status_bad();
}
